<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>C++11详解 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/26.74ba4989.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link router-link-active">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link router-link-active">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>C++11详解</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#📑-目录" class="sidebar-link">📑 目录</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1-智能指针4个『c-11』" class="sidebar-link">1.智能指针4个『C++11』</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1-1shared-ptr（共享指针）" class="sidebar-link">1.1shared_ptr（共享指针）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1-2-weak-ptr（弱指针）" class="sidebar-link">1.2 weak_ptr（弱指针）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1-3-unique-ptr（独享指针）" class="sidebar-link">1.3 unique_ptr（独享指针）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1-4-auto-ptr（弃用的）c-98标准" class="sidebar-link">1.4 auto_ptr（弃用的）C++98标准</a></li></ul></li><li><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_2-c-强制类型转换（type-cast）" class="sidebar-link">2.C++强制类型转换（Type Cast）</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1、背景" class="sidebar-link">1、背景</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_2、类型转换总览" class="sidebar-link">2、类型转换总览</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_3、static-cast" class="sidebar-link">3、static_cast</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_4、reinterpret-cast" class="sidebar-link">4、reinterpret_cast</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_5、dynamic-cast" class="sidebar-link">5、dynamic_cast</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_6、const-cast" class="sidebar-link">6、const_cast</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_7、总结" class="sidebar-link">7、总结</a></li></ul></li><li><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_3-c-11支持的正则表达式" class="sidebar-link">3.C++11支持的正则表达式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#正则表达式" class="sidebar-link">正则表达式</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_1）regex-match（正则匹配）" class="sidebar-link">1）regex_match（正则匹配）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_2）regex-search（正则搜索）" class="sidebar-link">2）regex_search（正则搜索）</a></li><li class="sidebar-sub-header"><a href="/01.Language/Cpp/02.C%2B%2B11%E8%AF%A6%E8%A7%A3.html#_3）regex-replace（正则替换）" class="sidebar-link">3）regex_replace（正则替换）</a></li></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="c-11详解"><a href="#c-11详解" class="header-anchor">#</a> C++11详解</h1> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>&lt;font style=&quot;background: yellow&quot;&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="📑-目录"><a href="#📑-目录" class="header-anchor">#</a> 📑 目录</h2> <p>[TOC]</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>智能指针
<span class="token number">1</span>、智能指针『『面试游戏公司的时候，问到过』』』
<span class="token number">2</span>、类型转换
<span class="token operator">-</span> <span class="token number">03.</span>匿名函数，lamda表达式『C<span class="token operator">++</span><span class="token number">11</span>』
C<span class="token operator">++</span><span class="token number">2.0</span> 是一个简称，意为「现代 C<span class="token operator">++</span>」，包括 C<span class="token operator">++</span><span class="token number">11</span><span class="token operator">/</span><span class="token number">14</span><span class="token operator">/</span><span class="token number">17</span><span class="token operator">/</span><span class="token number">20</span>。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="_1-智能指针4个『c-11』"><a href="#_1-智能指针4个『c-11』" class="header-anchor">#</a> 1.智能指针4个『C++11』</h2> <ul><li>智能指针（Smart pointer）</li> <li>是一种<code>pointer-like class</code>『行为像指针的class』</li> <li>智能指针是C++提供给我们的一种工具，用来管理对象的一个生命周期的，简单来说就是来帮我们管理动态内存（也就是heap）的</li></ul> <h3 id="_1-1shared-ptr（共享指针）"><a href="#_1-1shared-ptr（共享指针）" class="header-anchor">#</a> 1.1<code>shared_ptr</code>（共享指针）</h3> <ul><li>面试的时候读：share指针</li></ul> <p>设计思想：实现：共享式拥有（shared ownership）概念</p> <p>应用场景举例：程序需要在多个对象间『共享数据』（或者叫：允许多个对象共享相同的状态）</p> <ul><li>参考自《C++ primer》</li></ul> <p>缺点：</p> <p>shared_ptr指针要是碰到，两个对象互相指向，导致“<strong>循环引用</strong>”会导致“<font style="background:yellow;"><strong>内存泄漏</strong></font>”</p> <p>特点：</p> <ul><li>1、支持定制型删除器（custom deleter）</li> <li>2、可防范 Cross-DLL 问题（对象在动态链接库（DLL）中被 new 创建，却在另一个 DLL 内被 delete 销毁）</li> <li>3、自动解除互斥锁</li> <li>4、线程不安全</li></ul> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>线程安全分为两个方面：
	shared_ptr智能指针对象中的引用计数是多个智能指针对象共享的，两个线程中智能指针的引用计数同时进行++或者 - -操作，在为加锁的情况下，会导致计数混乱，这样有可能造成资源泄漏或者程序奔溃的问题，
	而且++和- -操作本身也不是原子的。智能指针管理的对象保存在堆上，两个线程同时去访问，也会导致线程安全问题。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="_1-1-1缺点的代码『环形引用』"><a href="#_1-1-1缺点的代码『环形引用』" class="header-anchor">#</a> 1.1.1缺点的代码『环形引用』</h4> <ul><li>参考<a href="https://blog.csdn.net/lijinqi1987/article/details/79005738" target="_blank" rel="noopener noreferrer">博客<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h4 id="_1-1-2shared-ptr的源代码『精华版』"><a href="#_1-1-2shared-ptr的源代码『精华版』" class="header-anchor">#</a> 1.1.2<code>shared_ptr</code>的源代码『精华版』</h4> <ul><li>参考源代码，自行写一份代码『模板版本』</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span> <span class="token keyword">typename</span> <span class="token class-name">T</span> <span class="token operator">&gt;</span>
<span class="token keyword">class</span> <span class="token class-name">shared_ptr</span>
<span class="token punctuation">{</span>
	<span class="token keyword">public</span><span class="token operator">:</span>
		T <span class="token operator">*</span> <span class="token function">make_shared</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> <span class="token function">use_count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">private</span> <span class="token operator">:</span>
		T <span class="token operator">*</span> ptr<span class="token punctuation">;</span>
		<span class="token keyword">int</span> <span class="token operator">*</span> count<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="_1-2-weak-ptr（弱指针）"><a href="#_1-2-weak-ptr（弱指针）" class="header-anchor">#</a> 1.2 <code>weak_ptr</code>（弱指针）</h3> <p>为了解决“循环引用”导致的“内存泄漏”，引入了weak_ptr指针。</p> <ul><li>《C++ Primer》一书中说它一个伴随类，它是一种<code>弱引用</code>，指向<code>shared_ptr</code>所管理的对象。</li></ul> <p>特点：将一个weak_ptr绑定到一个shared_ptr<strong>不会改变</strong>shared_ptr的引用计数！！</p> <ul><li>一旦最后1个指向对象的<code>shared_ptr</code>被销毁，对象就会被释放，即时有<code>weak_ptr</code>指向对象，对象也还是会被释放！！</li> <li>因此，weak_ptr的名字抓住了这种智能指针&quot;弱&quot;共享对象的特点！！</li></ul> <p>参考：</p> <ul><li>weak_ptr 允许你<strong>共享但不拥有某对象</strong>『『好像，你没有权利拥有<code>中科院文凭</code>，但是你有权利观察<code>某个学生的中科院文凭上的学号</code>，但是观察不代表什么』』，一旦最末一个拥有该对象的智能指针失去了所有权，任何 weak_ptr 都会自动成空（empty）。因此，在 default 和 copy 构造函数之外，weak_ptr 只提供 “接受一个 shared_ptr” 的构造函数。</li> <li>可打破环状引用（cycles of references，两个其实已经没有被使用的对象彼此互指，使之看似还在 “被使用” 的状态）的问题</li></ul> <h3 id="_1-3-unique-ptr（独享指针）"><a href="#_1-3-unique-ptr（独享指针）" class="header-anchor">#</a> 1.3 <code>unique_ptr</code>（独享指针）</h3> <ul><li>替换auto_ptr</li></ul> <p>前置知识点：C++11引入的术语，右值引用，move语义</p> <p>特点：</p> <ul><li>unique_ptr 是 C++11 才开始提供的类型，是一种在异常时可以帮助避免资源泄漏的智能指针。</li> <li>采用l『『独占式拥有』』，意味着可以确保<strong>一个对象和其相应的资源同一时间只被一个 pointer 拥有</strong>。</li> <li>一旦拥有着被销毁或编程 empty，或开始拥有另一个对象，先前拥有的那个对象就会被销毁，其任何相应资源亦会被释放。</li></ul> <table><thead><tr><th><code>unique_ptr</code></th> <th><code>auto_ptr</code></th></tr></thead> <tbody><tr><td>C++11引入</td> <td>C++98引入，C++11弃用</td></tr> <tr><td>1、无拷贝赋值语义2、但实现了<code>move</code> 语义；</td> <td>1、可以赋值拷贝 2、复制拷贝后所有权转移</td></tr> <tr><td>unique_ptr 可以管理数组（析构调用 <code>delete[]</code> ）；</td> <td>auto_ptr 对象不能管理数组（析构调用 <code>delete</code>）</td></tr></tbody></table> <h3 id="_1-4-auto-ptr（弃用的）c-98标准"><a href="#_1-4-auto-ptr（弃用的）c-98标准" class="header-anchor">#</a> 1.4 <code>auto_ptr</code>（弃用的）C++98标准</h3> <ul><li>C++98的时候的方案，C++11已抛弃</li> <li>采用所有权模式</li> <li>被 c++11 弃用，原因是缺乏语言特性如 “针对构造和赋值” 的 <code>std::move</code> 语义，以及其他瑕疵。</li></ul> <h2 id="_2-c-强制类型转换（type-cast）"><a href="#_2-c-强制类型转换（type-cast）" class="header-anchor">#</a> 2.C++强制类型转换（Type Cast）</h2> <h3 id="_1、背景"><a href="#_1、背景" class="header-anchor">#</a> 1、背景</h3> <h4 id="_1）场景1"><a href="#_1）场景1" class="header-anchor">#</a> 1）场景1</h4> <p>C语言由于某个强制类型转换出错了，我们需要查找所有代码中相应的强制类型转换，那么会很难寻找。比如：</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>double num=6.3131;
int solve=(int)num;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>原因</strong>：想要查找C语言风格的类型转换我们需要查找<code>（</code>这样的括号，但是函数也有啊<br> <strong>解决</strong>：C++中设计了4种类型转换，而且给他们每个后面都有单词cast<br>
以后，我们快速定位类型转换，直接用<code>Ctrl+F</code>去寻找<code>cast</code>就好了</p> <h4 id="_2）场景2"><a href="#_2）场景2" class="header-anchor">#</a> 2）场景2</h4> <p><strong>原因</strong>：由于C++中用基类和子类的指针，实现了多态。有时候，有些不熟悉多态的编码人员，可能会进行强制类型转换，导致项目崩溃。<br> <strong>解决</strong>：C++中针对这些，设计了比如<code>dynamic_cast</code>类型转换</p> <h3 id="_2、类型转换总览"><a href="#_2、类型转换总览" class="header-anchor">#</a> 2、类型转换总览</h3> <ul><li>C++相较C增加的<strong>新特性</strong></li></ul> <h4 id="_1）c-中4种类型转换如下"><a href="#_1）c-中4种类型转换如下" class="header-anchor">#</a> 1）C++中4种类型转换如下</h4> <ul><li>static_cast（静态类型转换）</li></ul> <blockquote><ul><li>静态类型转换，<strong>编译</strong>的时c++编译器会做<strong>类型检查</strong>，基本类型能转换但是<font style="background:yellow;"><strong>不能转换指针类型</strong></font></li></ul></blockquote> <ul><li>reinterpreter_cast（重新解释类型转换，interpreter，v.诠释，说明）</li></ul> <blockquote><ul><li>若不同类型之间，进行强制类型转换，用reinterpret_cast进行重新解释</li></ul></blockquote> <ul><li>dynamic_cast（动态类型转换）</li></ul> <blockquote><ul><li>C++中重要的，安全的基类和子类之间转换，<strong>运行时</strong>类型检查</li></ul></blockquote> <ul><li>const_cast（常量类型准换）</li></ul> <blockquote><ul><li>去除变量的只读属性</li></ul></blockquote> <h4 id="_2）一般结论"><a href="#_2）一般结论" class="header-anchor">#</a> 2）一般结论</h4> <ul><li>1.C语言中能<strong>隐式类型转换</strong>的，在c++中可用static_cast进行类型转换。因C++编译器在编译检查一般都能通过；</li> <li>2.C语言中<strong>不能隐式类型转换的</strong>，在c++中可以用reinterpret_cast进行强行类型解释。</li> <li>3.总结：static_cast和reinterpret_cast基本上把C语言中的 强制类型转换给覆盖，但是<strong>reinterpret_cast很难保证移植性。</strong></li></ul> <h3 id="_3、static-cast"><a href="#_3、static-cast" class="header-anchor">#</a> 3、static_cast</h3> <p>代码展示</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">double</span> pi <span class="token operator">=</span> <span class="token number">3.1415926</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> num1 <span class="token operator">=</span> <span class="token keyword">static_cast</span><span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>pi<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//c++的新式的类型转换运算符  </span>
<span class="token keyword">int</span> num2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span>pi<span class="token punctuation">;</span><span class="token comment">//“强制”类型转换 </span>
<span class="token keyword">int</span> num3 <span class="token operator">=</span> pi<span class="token punctuation">;</span><span class="token comment">//c语言/C++的“隐式”类型转换</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>静态的类型转换:  在<strong>编译</strong>的时进行基本类型的转换 能替代c风格的类型转换，可以进行一部分检查</li></ul> <h3 id="_4、reinterpret-cast"><a href="#_4、reinterpret-cast" class="header-anchor">#</a> 4、reinterpret_cast</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">char</span> <span class="token operator">*</span>p1 <span class="token operator">=</span> <span class="token string">&quot;hello wangbaoming &quot;</span> <span class="token punctuation">;</span>
<span class="token keyword">int</span> <span class="token operator">*</span>p2 <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
p2 <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token operator">*</span><span class="token punctuation">)</span>p1<span class="token punctuation">;</span>
<span class="token comment">//static_cast对于基本类型可以转换，但是不能转换指针类型！</span>
<span class="token comment">//编译的时候就会报错：[Error] invalid static_cast from type 'char*' to type 'int*'</span>

<span class="token comment">//可以使用  reinterpret_cast 进行重新解释 </span>
p2 <span class="token operator">=</span> <span class="token keyword">reinterpret_cast</span><span class="token operator">&lt;</span><span class="token keyword">int</span> <span class="token operator">*</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>p1<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_5、dynamic-cast"><a href="#_5、dynamic-cast" class="header-anchor">#</a> 5、dynamic_cast</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">Animal</span> 
<span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
	<span class="token keyword">virtual</span> <span class="token keyword">void</span>  <span class="token function">voice</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span><span class="token punctuation">;</span>


<span class="token keyword">class</span> <span class="token class-name">Dog</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Animal</span></span> 
<span class="token punctuation">{</span>
	<span class="token keyword">public</span><span class="token operator">:</span>
		<span class="token keyword">virtual</span> <span class="token keyword">void</span>  <span class="token function">voice</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;汪汪&quot;</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
		<span class="token punctuation">}</span>

		<span class="token keyword">void</span> <span class="token function">doSwim</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  
		<span class="token punctuation">{</span> 
			cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;狗游泳&quot;</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span> 
		<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>



<span class="token keyword">class</span> <span class="token class-name">Cat</span> <span class="token operator">:</span> <span class="token base-clause"><span class="token keyword">public</span> <span class="token class-name">Animal</span></span>
<span class="token punctuation">{</span>
	<span class="token keyword">public</span><span class="token operator">:</span>
		<span class="token keyword">virtual</span> <span class="token keyword">void</span>  <span class="token function">voice</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;喵喵 &quot;</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		<span class="token keyword">void</span> <span class="token function">doTree</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  
		<span class="token punctuation">{</span> 
			cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;猫爬树&quot;</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span> 
		<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>




<span class="token comment">//另一个类群体 </span>
<span class="token keyword">class</span> <span class="token class-name">Book</span>
<span class="token punctuation">{</span>
	<span class="token keyword">private</span><span class="token operator">:</span>
		<span class="token keyword">int</span> price<span class="token punctuation">;</span>
		
	<span class="token keyword">public</span><span class="token operator">:</span>
		<span class="token keyword">void</span> <span class="token function">printP</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			cout <span class="token operator">&lt;&lt;</span> price <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>



<span class="token comment">//对象运行 </span>
<span class="token keyword">void</span> <span class="token function">ObjPlay</span><span class="token punctuation">(</span>Animal <span class="token operator">*</span>base<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	base<span class="token operator">-&gt;</span><span class="token function">voice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token comment">//猫没有转换成功 </span>
	Dog <span class="token operator">*</span>pDog <span class="token operator">=</span> <span class="token keyword">dynamic_cast</span><span class="token operator">&lt;</span>Dog <span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>base<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> pDog<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		pDog<span class="token operator">-&gt;</span><span class="token function">voice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		pDog<span class="token operator">-&gt;</span><span class="token function">doSwim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>


	Cat <span class="token operator">*</span>pCat <span class="token operator">=</span> <span class="token keyword">dynamic_cast</span><span class="token operator">&lt;</span>Cat <span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>base<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token constant">NULL</span> <span class="token operator">!=</span> pCat<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		pCat<span class="token operator">-&gt;</span><span class="token function">voice</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		pCat<span class="token operator">-&gt;</span><span class="token function">doTree</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span>  <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment">//基类指针base </span>
	Animal <span class="token operator">*</span>base <span class="token operator">=</span> <span class="token constant">NULL</span><span class="token punctuation">;</span>
	

	<span class="token comment">//1）可以把子类指针赋给 父类指针 但是反过来是不可以的 需要 如下转换</span>
	<span class="token comment">//pdog = base;  </span>
	Dog <span class="token operator">*</span>pDog <span class="token operator">=</span> <span class="token keyword">static_cast</span><span class="token operator">&lt;</span>Dog <span class="token operator">*</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>base<span class="token punctuation">)</span><span class="token punctuation">;</span>


	<span class="token comment">//2）把base转换成其他 非动物相关的 err</span>
	<span class="token comment">//比如下面这一行在编译时会出错！ </span>
	<span class="token comment">//Book *book= static_cast&lt;Book *&gt; (base);</span>


	<span class="token comment">//3  reinterpret_cast //可以强制类型转换</span>
	Book <span class="token operator">*</span>book2<span class="token operator">=</span> <span class="token keyword">reinterpret_cast</span><span class="token operator">&lt;</span>Book <span class="token operator">*</span><span class="token operator">&gt;</span> <span class="token punctuation">(</span>base<span class="token punctuation">)</span><span class="token punctuation">;</span>


	<span class="token comment">//4 dynamic_cast用法</span>
	<span class="token function">ObjPlay</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token function">Cat</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;pause&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br></div></div><p>运行结果是</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>喵喵
喵喵
猫爬树
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="bad-cast"><a href="#bad-cast" class="header-anchor">#</a> bad_cast</h4> <ul><li>由于转换为『引用类型』失败，<code>dynamic_cast</code>运算符会引发<code>bad_cast</code>异常</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">try</span>
<span class="token punctuation">{</span>
    Circle <span class="token operator">&amp;</span> ref_circle <span class="token operator">=</span> <span class="token keyword">dynamic_cast</span><span class="token operator">&lt;</span>Circle<span class="token operator">&amp;</span><span class="token operator">&gt;</span><span class="token punctuation">(</span> ref_shape <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">catch</span> <span class="token punctuation">(</span> bad_cast b <span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    cout<span class="token operator">&lt;&lt;</span> <span class="token string">&quot;Caught:&quot;</span> <span class="token operator">&lt;&lt;</span> b<span class="token punctuation">.</span><span class="token function">what</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><blockquote><p>参考：<a href="https://github.com/huihut/interview" target="_blank" rel="noopener noreferrer">https://github.com/huihut/interview<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></blockquote> <h3 id="_6、const-cast"><a href="#_6、const-cast" class="header-anchor">#</a> 6、const_cast</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>


<span class="token comment">//典型用法，把形参的只读属性去掉</span>
<span class="token keyword">void</span> <span class="token function">Opbuf</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>p<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	cout <span class="token operator">&lt;&lt;</span> p <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
	
	<span class="token comment">//把形参的只读属性去掉 </span>
	<span class="token keyword">char</span> <span class="token operator">*</span>p2 <span class="token operator">=</span> <span class="token keyword">const_cast</span><span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>p<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	p2<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'A'</span><span class="token punctuation">;</span>
	cout <span class="token operator">&lt;&lt;</span> p <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>


<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span>p1 <span class="token operator">=</span> <span class="token string">&quot;11111111111&quot;</span><span class="token punctuation">;</span>
	<span class="token keyword">char</span> <span class="token operator">*</span>p2 <span class="token operator">=</span> <span class="token string">&quot;333333333&quot;</span><span class="token punctuation">;</span>
	
	<span class="token comment">//下面就是用法，虽然会显示</span>
	<span class="token comment">//[Warning] deprecated conversion from string constant to 'char*' [-Wwrite-strings] </span>
	<span class="token keyword">char</span> <span class="token operator">*</span>p3 <span class="token operator">=</span> <span class="token keyword">const_cast</span><span class="token operator">&lt;</span><span class="token keyword">char</span> <span class="token operator">*</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>p1<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	
	
	<span class="token comment">//下面 &quot;000000000000&quot;会自动拆解，放入buf数组中 </span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">&quot;000000000000&quot;</span><span class="token punctuation">;</span>

	<span class="token function">Opbuf</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">//要保证指针所执行的内存空间能修改才行 若不能修改 还是会引起程序异常！！！</span>
	<span class="token comment">//比如下面,因为下面的 &quot;dddddddddddsssssssssssssss&quot;存在常量区 </span>
	<span class="token comment">//注意和 char buf[100] = &quot;000000000000&quot;;区别 </span>
	<span class="token comment">//	Opbuf(&quot;dddddddddddsssssssssssssss&quot;);</span>

	<span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;pause&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><div class="language-txt line-numbers-mode"><pre class="language-text"><code>000000000000
A00000000000
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="_7、总结"><a href="#_7、总结" class="header-anchor">#</a> 7、总结</h3> <ul><li>1）编码人员要清楚的知道: 要转的变量，类型转换前是什么类型，类型转换后是什么类型。转换后有什么后果。</li> <li>2）一般情况下，不建议进行类型转换；避免进行类型转换。</li> <li>比如上面的“把形参的只读属性去掉”，我在C语言编程的时候，本来就是怕别人写的模块修改我传入的参数才用的const，但是这种类型转换会破坏我设计的初衷！</li></ul> <h2 id="_3-c-11支持的正则表达式"><a href="#_3-c-11支持的正则表达式" class="header-anchor">#</a> 3.C++11支持的正则表达式</h2> <ul><li>（对付CSP第3题）</li></ul> <p>正则的编译时比较慢的，我们暂时
字符编码，我们的控制台的字符编码是GBK的，所以，要是能够改为UTF-8就不会乱码</p> <p><font style="background:yellow;">注意记得开启C++11编译选项和<code>#include &lt;regex&gt;</code></font></p> <p><a href="https://zh.cppreference.com/w/cpp/regex" target="_blank" rel="noopener noreferrer">链接:看regex头文件定义的传送门<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p> <p>注意<code>C++11</code>新增了正则表达式的标准库支持
在 <code>C++</code> 中使用正则表达式，和其它语言差别不大
<code>C++11</code> 自带了 6 种正则表达式语法的支持</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>ECMAScript
basic
extended
awk
grep
egrep
C++11 默认使用 ECMAScript 语法，这也是 6 种语法中最强大的，假如想使用其他 5 种语法，只需在声明 regex 对象时指定即可
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>C++正则表达式最常用的三个函数为，也是一般用法。
正则表达式算是字符串匹配中的一个微型元。</p> <h3 id="正则表达式"><a href="#正则表达式" class="header-anchor">#</a> 正则表达式</h3> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>.表示任意匹配单个字符
*表示匹配0-n个前面那个字符串
+是1-n次
[a-z]区间，a-z
\d{3}表示匹配3个数字，固定是3个
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_1）regex-match（正则匹配）"><a href="#_1）regex-match（正则匹配）" class="header-anchor">#</a> 1）regex_match（正则匹配）</h3> <p>尝试匹配一个正则表达式到整个字符序列(函数模板)</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;regex&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment">//正则表达式，也就是匹配规则	 </span>
	<span class="token comment">//R的意思是取消转义，这样我们就能在里面不用转义了。否则下面的\d要改为\\d </span>
	regex <span class="token function">test</span><span class="token punctuation">(</span><span class="token raw-string string">R&quot;(\d*)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	string str<span class="token operator">=</span><span class="token string">&quot;43141324&quot;</span><span class="token punctuation">;</span><span class="token comment">//现在全是数字，能够匹配上，输出是OK</span>
    
    <span class="token keyword">bool</span> t<span class="token punctuation">;</span>
    t<span class="token operator">=</span><span class="token function">regex_match</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//前面是要匹配的，后面是正则表达式（匹配规则）</span>
    <span class="token comment">//但是如果是&quot;123okom231&quot;这样，用前面的正则表达式，那么结果是No</span>
	
	<span class="token keyword">if</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		cout<span class="token operator">&lt;&lt;</span><span class="token string">&quot;OK&quot;</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token punctuation">}</span> 
	<span class="token keyword">else</span>
	<span class="token punctuation">{</span>
		cout<span class="token operator">&lt;&lt;</span><span class="token string">&quot;No&quot;</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
    
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;regex&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment">//正则表达式，也就是匹配规则	 </span>
	<span class="token comment">//R的意思是取消转义，这样我们就能在里面不用转义了。否则下面的\d要改为\\d </span>
	regex <span class="token function">test</span><span class="token punctuation">(</span><span class="token raw-string string">R&quot;(\d+[a-z]{2}\d+)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	string str<span class="token operator">=</span><span class="token string">&quot;4314er1324&quot;</span><span class="token punctuation">;</span>
    
    <span class="token keyword">bool</span> t<span class="token punctuation">;</span>
    t<span class="token operator">=</span><span class="token function">regex_match</span><span class="token punctuation">(</span>str<span class="token punctuation">,</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//前面是要匹配的，后面是正则表达式（匹配规则）</span>
	
	<span class="token keyword">if</span><span class="token punctuation">(</span>t<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		cout<span class="token operator">&lt;&lt;</span><span class="token string">&quot;OK&quot;</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token punctuation">}</span> 
	<span class="token keyword">else</span>
	<span class="token punctuation">{</span>
		cout<span class="token operator">&lt;&lt;</span><span class="token string">&quot;No&quot;</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
    
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h3 id="_2）regex-search（正则搜索）"><a href="#_2）regex-search（正则搜索）" class="header-anchor">#</a> 2）regex_search（正则搜索）</h3> <h3 id="_3）regex-replace（正则替换）"><a href="#_3）regex-replace（正则替换）" class="header-anchor">#</a> 3）regex_replace（正则替换）</h3></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/26.74ba4989.js" defer></script>
  </body>
</html>
